package com.fedorvlasov.lazylist;


import java.util.ArrayList;

import androidstore.app.data.AppData;
import androidstore.app.data.AppFetcher;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends Activity  {
    ListView list;
    ListView list_free;
    LazyAdapter adapter;
    Button btSearch;
    EditText edtSearch;
    private static final int MENU_SET_SATELLITE = 1;
    private static final int MENU_SET_MAP = 2;
    private static final int MENU_AppS_FROM_MAP_CENTER = 3;
    private static final int MENU_BACK_TO_LAST_LOCATION = 4;
    //private ProgressDialog progressDialog;
    //private final Context context;
    ArrayList<AppData> PaidAppList;
    ArrayList<AppData> FreeAppList;
    AppFetcher fetcher;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        fetcher = new AppFetcher();
        PaidAppList = fetcher.getTopApp(0);
        list=(ListView)findViewById(R.id.list);
        adapter=new LazyAdapter(this, PaidAppList);
        btSearch = (Button)findViewById(R.id.btSearch);
        edtSearch = (EditText)findViewById(R.id.edtSearch);
        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) 
            {
            	//AppActivity.appdata = PaidAppList.get(position);
            	Intent intent = new Intent(getApplicationContext(),AppActivity.class);
            	//intent.setClass();
            	AppActivity.appdata = PaidAppList.get(position);
            	startActivity(intent);
             }
              });

        list.setAdapter(adapter);

        FreeAppList = fetcher.getTopApp(1);
        list_free=(ListView)findViewById(R.id.list_free);
        adapter=new LazyAdapter(this, FreeAppList);
        list_free.setAdapter(adapter); 
        list_free.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) 
            {
            	//AppActivity.appdata = PaidAppList.get(position);
            	Intent intent = new Intent(getApplicationContext(),AppActivity.class);
            	//intent.setClass();
            	AppActivity.appdata = FreeAppList.get(position);
            	startActivity(intent);
             }
              });
        
        btSearch.setOnClickListener(btSearchListener);
        Button b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(listener);
        
        Button bt_Game=(Button)findViewById(R.id.bt_Game);
        bt_Game.setOnClickListener(btMenuListener);    
        
        Button bt_Entertainment= (Button)findViewById(R.id.bt_Entertainment);
        bt_Entertainment.setOnClickListener(btMenuListener);             
    }
    
    @Override
    public void onDestroy()
    {
        adapter.imageLoader.stopThread();
        list.setAdapter(null);
        super.onDestroy();
    }
    
    public OnClickListener btMenuListener = new OnClickListener(){
    	@Override
    	public void onClick(View v){
    		String categoryName = null;
			Intent intent = new Intent(getApplicationContext(),AppListActivity.class);
			categoryName = ((Button)v).getText().toString();
			AppListActivity.categoryName = categoryName;
			startActivity(intent);			
    	}	
    };
    
    public OnClickListener btSearchListener = new OnClickListener(){
    	@Override
    	public void onClick(View v){
    		String searchword = null;
			Intent intent = new Intent(getApplicationContext(),SearchListActivity.class);
			searchword = edtSearch.getText().toString();
			SearchListActivity.searchword = searchword;
			startActivity(intent);			
    	}	
    }; 
    
    public OnClickListener listener=new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            adapter.imageLoader.clearCache();
            adapter.notifyDataSetChanged();
        }
    };
    
  
    
    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, MainActivity.MENU_SET_MAP, 0, "Map").setIcon(android.R.drawable.ic_menu_mapmode);
        menu.add(0, MainActivity.MENU_SET_SATELLITE, 0, "Satellite").setIcon(android.R.drawable.ic_menu_mapmode);
        menu.add(1, MainActivity.MENU_AppS_FROM_MAP_CENTER, 0, "Tìm công việc").setIcon(
            android.R.drawable.ic_menu_directions);
        menu.add(2, MainActivity.MENU_BACK_TO_LAST_LOCATION, 0, "Vị trí của tôi").setIcon(
            android.R.drawable.ic_menu_mylocation);
        return true;
    }
    

    @Override
    public boolean onMenuItemSelected(final int featureId, final MenuItem item) {
        switch (item.getItemId()) {
            case MainActivity.MENU_SET_MAP:
                break;
            case MainActivity.MENU_SET_SATELLITE:

                break;
            case MainActivity.MENU_AppS_FROM_MAP_CENTER:

                break;
            case MainActivity.MENU_BACK_TO_LAST_LOCATION:
                break;
        }
        return super.onMenuItemSelected(featureId, item);
    }    
    




}